<?php
require(__MODEL_GEN__ . '/OrgGen.class.php');

/**
 * The Org class defined here contains any
 * customized code for the Org class in the
 * Object Relational Model.  It represents the "org" table
 * in the database, and extends from the code generated abstract OrgGen
 * class, which contains all the basic CRUD-type functionality as well as
 * basic methods to handle relationships and index-based loading.
 *
 * @package My QCubed Application
 * @subpackage DataObjects
 *
 */
class Org extends OrgGen {
    /**
     * Default "to string" handler
     * Allows pages to _p()/echo()/print() this object, and to define the default
     * way this object would be outputted.
     *
     * Can also be called directly via $objOrg->__toString().
     *
     * @return string a nicely formatted string representation of this object
     */
    public function __toString() {
        return sprintf('%s',  $this->Name);
    }

    public static function LoadArrayMyOrgPRole($objOptionalClauses = null) {
        $PRole = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        try {
            return Org::QueryArray(
                    QQ::OrCondition(
                    QQ::Equal(QQN::Org()->SalesId , $PRole),
                    QQ::Equal(QQN::Org()->ConsultantId , $PRole)
                    ),
                    $objOptionalClauses);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    public static function LoadArrayMyOrgERoles($objOptionalClauses = null) {
        $ArrayRole="";
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ERole ) {
            if (isset( $ERole ) )
                $ArrayRole[] .= $ERole->Id;
        }

        if ( $ArrayRole == "" )
            return array();
        try {
            return Org::QueryArray(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole)
                    ),
                    $objOptionalClauses);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    public static function LoadArrayMyOrgAllRoles($objOptionalClauses = null) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }

        try {
            return Org::QueryArray(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole)
                    ),
                    $objOptionalClauses);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    public static function LoadArrayMyOrgAllPerm($objOptionalClauses = null) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }

        try {
            return Org::QueryArray(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    $objOptionalClauses);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    public static function LoadArrayMyOrgAllPermCondition(QQCondition $objConditions, $objOptionalClauses = null) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }
        try {
            return Org::QueryArray(
                    QQ::AndCondition(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    $objConditions
                    ),
                    $objOptionalClauses);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    public static function CountMyOrgAllPermCondition(QQCondition $objConditions) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }

        try {
            return Org::QueryCount(
                    QQ::AndCondition(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    $objConditions
            ));
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }

    public static function CheckOrgPerm($OrgId) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }

        try {
            return Org::QueryArray(
                    QQ::AndCondition(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    QQ::Equal(QQN::Org()->Id,$OrgId )
            ));
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
    }
    //Auto Complete List
    public static function AutoListMySchoolList( $strParameter, $Return_Type ) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }
        try {
            $objMemberArray =  Org::QueryArray(
                    QQ::AndCondition(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    QQ::OrCondition(
                    QQ::Equal(QQN::Org()->TypeId, OrgType::Customer),
                    QQ::Equal(QQN::Org()->TypeId, OrgType::BookSeller),
                    QQ::Equal(QQN::Org()->TypeId, OrgType::Publisher)
                    ),
                    QQ::Like(QQN::Org()->Name, $strParameter . '%')
                    ),
                    QQ::Clause(QQ::OrderBy(QQN::Org()->Name))
            );
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        $result = array();
        if ($Return_Type == "Name")
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->Name. "|" . $objMember->Id;
            }
        else
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->OracleLink;
            }
        return $result;

    }

    public static function AutoListAllSchoolList( $strParameter, $Return_Type ) {
        try {
            $objMemberArray =  Org::QueryArray(
                    QQ::AndCondition(
                    QQ::Equal(QQN::Org()->TypeId, OrgType::Customer),
                    QQ::Like(QQN::Org()->Name, $strParameter . '%')),
                    QQ::Clause(QQ::OrderBy(QQN::Org()->Name))
            );
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        $result = array();
        if ($Return_Type == "Name")
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->Name. "|" . $objMember->Id;
            }
        else
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->OracleLink;
            }

        return $result;
        
    }


    public static function AutoListMyOrgList( $strParameter, $Return_Type ) {
        $ArrayRole[] = $_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id;
        foreach ( $_SESSION[_SV_PERM_][_SV_PERM_ERole_] as $ORole ) {
            if (isset( $ORole ) )
                $ArrayRole[] .=  $ORole->Id;
        }
        try {
            $objMemberArray =  Org::QueryArray(
                    QQ::AndCondition(
                    QQ::OrCondition(
                    QQ::In(QQN::Org()->SalesId , $ArrayRole),
                    QQ::In(QQN::Org()->ConsultantId , $ArrayRole),
                    QQ::Equal(QQN::Org()->RoleAsMano->RoleId ,$_SESSION[_SV_PERM_][_SV_PERM_PRole_]->Id )
                    ),
                    QQ::Like(QQN::Org()->Name, $strParameter . '%')
                    ),
                    QQ::Clause(QQ::OrderBy(QQN::Org()->Name))
            );
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        $result = array();
        if ($Return_Type == "Name")
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->Name;
            }
        else
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->OracleLink;
            }
        return $result;

    }

    public static function AutoListAllOrgList( $strParameter, $Return_Type ) {
        try {
            $objMemberArray =  Org::QueryArray(
                                            QQ::Like(QQN::Org()->Name, $strParameter . '%'),
                                            QQ::Clause(QQ::OrderBy(QQN::Org()->Name) )
                                    );
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        $result = array();
        if ($Return_Type == "Name")
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->Name;
            }
        else
            foreach($objMemberArray as $objMember) {
                $result[] = $objMember->OracleLink;
            }

        return $result;

    }

}
?>